package com.exercise.memberscheduling.tools;

import com.exercise.memberscheduling.domain.valueObject.Member;
import com.exercise.memberscheduling.repository.service.MemberService;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class ExportExcelUtils {
    @Autowired
    private MemberService service;
    public HSSFWorkbook createExcel()
    {
        String []header = {"姓名","剩余课程数"};
        //声明一个表格
        HSSFWorkbook workbook = new HSSFWorkbook();

        //生成一个sheet，
        HSSFSheet sheet = workbook.createSheet("会员信息表");
        //设置表格列宽度为10个字节
        sheet.setDefaultColumnWidth(10);
        //创建标题的显示样式
        HSSFCellStyle headerStyle = workbook.createCellStyle();
        headerStyle.setFillForegroundColor(IndexedColors.YELLOW.index);
        headerStyle.setFillPattern(FillPatternType.SOLID_FOREGROUND);
        //创建第一行表头
        HSSFRow headrow = sheet.createRow(0);
        //遍历添加表头
        for (int i = 0; i < header.length; i++) {
            //创建一个单元格
            HSSFCell cell = headrow.createCell(i);

            //创建一个内容对象
            HSSFRichTextString text = new HSSFRichTextString(header[i]);

            //将内容对象的文字内容写入到单元格中
            cell.setCellValue(text);
            cell.setCellStyle(headerStyle);
        }
        List<Member> memberList = service.selAllMembers();
        int i = 0;
        for(Member m:memberList)
        {
            //创建一行
            HSSFRow row1 = sheet.createRow(i+1);
            row1.createCell(0).setCellValue(new HSSFRichTextString(m.getMemberName()));
            row1.createCell(1).setCellValue(new HSSFRichTextString(String.valueOf(m.getRemainTimes())));
            i++;
        }
        return workbook;
    }
}
